package blog.open1111.controller.admin;

import blog.open1111.entity.Manager;
import blog.open1111.service.ManagerService;
import blog.open1111.util.Md5Util;
import blog.open1111.util.ResponseUtil;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;

/**
 * Created by dengwubo on 2017/9/8.
 */

@SuppressWarnings("all")
@Controller
@RequestMapping("/admin/manager")
public class ManagerAdminController {
    @Autowired
    private ManagerService managerService;

    /**
     * 修改管理员密码
     * @param newPassword
     * @param id
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/modifyPassword")
    public String modifyPassword(String newPassword,Integer id, HttpServletResponse response) throws Exception{
        Manager manager = new Manager();
        String md5 = Md5Util.md5(newPassword, Md5Util.SALT);
        System.out.println(md5);
        manager.setPassword(md5);
        manager.setId(id);
        int resultTotal = managerService.update(manager);
        JSONObject result = new JSONObject();
        if (resultTotal>0){
            result.put("success",true);
        }else{
            result.put("success",false);
        }
        ResponseUtil.write(response,result);
        return null;
    }

    @RequestMapping("/logout")
    public String logout() throws Exception{
        SecurityUtils.getSubject().logout();

        return "redirect:/login.jsp";
    }

}
